package liujia.bt.display.core
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import liujia.bt.display.BtDisplay;
	import liujia.bt.display.interf.IBtMovieClip;
	
	/**
	 * BtAnalyse
	 * @author 刘佳 liujia08@snda.com
	 * $Id: BtAnalyse.as 2 2011-10-31 01:46:19Z sigmaliu@163.com $
	 * @version 1.0
	 */
	public class BtAnalyse
	{
		/**
		 *弧度转角度 
		 */		
		public static const radians2rotation:Number = 180 / Math.PI;
		
		/**
		 *角度转弧度 
		 */		
		public static const rotation2radians:Number = Math.PI / 180;
		
		/**
		 *把MovieClip的每帧转成BtFrame
		 * @param $mc 源MovieClip
		 * @return 转成的BtFrame存放在一个Vector里
		 * 
		 */		
		public static function mcToBitmap($mc:MovieClip):Vector.<BtFrame> {
			var btFrameVec:Vector.<BtFrame> = new Vector.<BtFrame>;
			for (var i:int=1; i<=$mc.totalFrames; i++) {
				$mc.gotoAndStop(i);
				var btFrame:BtFrame = createFrame($mc);
				btFrameVec.push(btFrame);
			}
			
			for (i=btFrameVec.length-1; i>0; --i) {
				var currFrame:BtFrame = btFrameVec[i];
				for (var j:int=btFrameVec.length-1; j>0; --j) {
					var compareFrame:BtFrame = btFrameVec[j];
					var compare:Object = currFrame.bitmapData.compare(compareFrame.bitmapData);
					if (compare==0 && compareFrame.bitmapData!=currFrame.bitmapData) {
						compareFrame.bitmapData.dispose();
						compareFrame.bitmapData = null;
						compareFrame.rect = null;
						btFrameVec[j] = currFrame;
					}
				}
			}
			
			return btFrameVec;
		}

		//此方法已由另一个更快更强的同名方法代替
//		/**
//		 *单幅画面转成一个BtFrame
//		 * @param dp 源DisplayObject，如果是MovieClip，转出来的就是当前帧的画面
//		 * @return 转成的BtFrame
//		 * 
//		 */
//		public static function createFrame(dp:DisplayObject):BtFrame {
//			var rect:Rectangle = dp.getBounds(dp);
//			rect.x = Math.floor(rect.x);
//			rect.y = Math.floor(rect.y);
//			rect.width = Math.ceil(rect.width);
//			rect.height = Math.ceil(rect.height);
//			
//			var bitmapData:BitmapData = null;
//			
//			var matrix:Matrix = new Matrix(dp.scaleX, 0, 0, dp.scaleY, -rect.left*dp.scaleX, -rect.top*dp.scaleY);
//			matrix.rotate(dp.rotation * rotation2radians);
//			var p0:Point = matrix.transformPoint(rect.topLeft);
//			var p1:Point = matrix.transformPoint(new Point(rect.right, rect.top));
//			var p2:Point = matrix.transformPoint(rect.bottomRight);
//			var p3:Point = matrix.transformPoint(new Point(rect.left, rect.bottom));
//			var x_max:Number = Math.max(p0.x, p1.x, p2.x, p3.x);
//			var x_min:Number = Math.min(p0.x, p1.x, p2.x, p3.x);
//			var y_max:Number = Math.max(p0.y, p1.y, p2.y, p3.y);
//			var y_min:Number = Math.min(p0.y, p1.y, p2.y, p3.y);
//			var w:Number = x_max - x_min;
//			var h:Number = y_max - y_min;
//			matrix.translate(-x_min, -y_min);
//			
//			var newRegPoint:Point = matrix.deltaTransformPoint(new Point(-rect.x, -rect.y));
//			rect = new Rectangle(-(newRegPoint.x-x_min), -(newRegPoint.y-y_min), w, h);
//			
//			if (rect.width == 0 || rect.height == 0) {
//				bitmapData = new BitmapData(1, 1, BtDisplay.transparent, 0);
//			} else {
//				bitmapData = new BitmapData(w, h, BtDisplay.transparent, 0);
//			}
//			
//			bitmapData.draw(dp, matrix);
//			
//			return new BtFrame(bitmapData, rect);
//		}
		
		/**
		 *单幅画面转成一个BtFrame
		 * @param dp 源DisplayObject, dp画面的平移、缩放、旋转、倾斜、注册点、滤镜将还原到BtFrame中
		 * @return 转成的BtFrame，如果是MovieClip，转出来的就是当前帧的画面
		 * 
		 */		
		public static function createFrame(dp:DisplayObject):BtFrame {
			var rect:Rectangle = dp.getBounds(BtRun.stage);
			rect.x = Math.round(rect.x);
			rect.y = Math.round(rect.y);
			rect.width = Math.ceil(rect.width);
			rect.height = Math.ceil(rect.height);
			
			if (dp.filters!=null) {
				var blurX:Number = 0;
				var blurY:Number = 0;
				for each (var filter:Object in dp.filters) {
					if (filter.hasOwnProperty("blurX") && filter.blurX>blurX) blurX = filter.blurX;
					if (filter.hasOwnProperty("blurY") && filter.blurY>blurY) blurY = filter.blurY;
				}
				rect.x = rect.x-Math.floor(blurX/2);
				rect.y = rect.y-Math.floor(blurY/2);
				rect.width = rect.width+Math.ceil(blurX+2);
				rect.height = rect.height+Math.ceil(blurY+2);
			}
			
			var matrix:Matrix = dp.transform.matrix.clone();
			matrix.tx = -rect.x;
			matrix.ty = -rect.y;
			
			var bitmapData:BitmapData = null;
			if (rect.width == 0 || rect.height == 0) {
				rect.width = rect.height = 1;
			}
			bitmapData = new BitmapData(rect.width, rect.height, BtDisplay.transparent, 0);
			
			bitmapData.draw(dp, matrix);
			
			return new BtFrame(bitmapData, rect);
		}
		
		/**
		 *深克隆BtFrame，并在克隆的过程中进行缩放和旋转, 不共享bitmapData
		 * @param sourceBtFrame 源BtFrame
		 * @param scaleX 水平缩放
		 * @param scaleY 垂直缩放
		 * @param rotation 角度
		 * @return 克隆出来的BtFrame
		 * 
		 */		
		public static function deepCloneBtFrame(sourceBtFrame:BtFrame, scaleX:Number, scaleY:Number, rotation:Number):BtFrame {
			var rect:Rectangle = sourceBtFrame.rect;
			
			var matrix:Matrix = new Matrix(scaleX, 0, 0, scaleY, -rect.left*scaleX, -rect.top*scaleY);
			matrix.rotate(rotation * rotation2radians);
			var p0:Point = matrix.transformPoint(rect.topLeft);
			var p1:Point = matrix.transformPoint(new Point(rect.right, rect.top));
			var p2:Point = matrix.transformPoint(rect.bottomRight);
			var p3:Point = matrix.transformPoint(new Point(rect.left, rect.bottom));
			var x_max:Number = Math.max(p0.x, p1.x, p2.x, p3.x);
			var x_min:Number = Math.min(p0.x, p1.x, p2.x, p3.x);
			var y_max:Number = Math.max(p0.y, p1.y, p2.y, p3.y);
			var y_min:Number = Math.min(p0.y, p1.y, p2.y, p3.y);
			var w:Number = x_max - x_min;
			var h:Number = y_max - y_min;
			matrix.translate(-x_min, -y_min);
			
			var newRegPoint:Point = matrix.deltaTransformPoint(new Point(-rect.x, -rect.y));
			rect = new Rectangle(Math.round(x_min-newRegPoint.x), Math.round(y_min-newRegPoint.y), w, h);
			
			matrix.tx = Math.round(-x_min);
			matrix.ty = Math.round(-y_min);
			
			var bitmapData:BitmapData = new BitmapData(rect.width, rect.height, true, 0);
			
			bitmapData.draw(sourceBtFrame.bitmapData, matrix);
			
			return new BtFrame(bitmapData, rect);
		}
		
	}
}
